@extends('admin.base')
@section('content')
    <style>
        .table-header-fixed {
            top: 0;
            position: fixed;
            z-index: 9999999;
        }
    </style>

    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
    <div class="layui-card">
        <legend>&nbsp;</legend>
        <div style="margin: 10px 10px 10px 10px;">
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <input type="text" style="width: 250px;" id="customer_name" name="customer_name" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">商品编号</label>
                <div class="layui-input-inline">
                    <input type="text" style="width: 250px;" id="product_code" name="product_code" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-inline">
                    <label class="layui-form-label">日期</label>
                    <div class="layui-input-inline">
                        <input type="text" id="date" name="date" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-primary" id="Search"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </div>


        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="approval">审批</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                </div>
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('supplier.manage')
        <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
        <script>
            layui.use(['layer', 'table', 'form', 'upload', 'element'], function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                var upload = layui.upload;
                var element = layui.element;

                layui.use('laydate', function() {
                    var laydate = layui.laydate;
                    laydate.render({
                        elem: '#date', // 绑定的元素选择器
                    });
                });

                $(".layui-table").find('td').data('edit', false);
                // 用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable',
                    url: "{{ route('admin.supplier.details.data') }}", // 数据接口
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    page: true, // 开启分页
                    cols: [[ // 表头
                        { checkbox: true, fixed: true },
                        { field: 'id', title: 'ID'},
                        { field: 'date_time', title: '日期'},
                        { field: 'order_number', title: '单号'},
                        { field: 'customer_name', title: '客户名称'},
                        { field: 'product_code', title: '商品编号'},
                        { field: 'product_name', title: '品名'},
                        { field: 'specification', title: '规格' },
                        { field: 'brand', title: '厂牌'},
                        { field: 'batch_number', title: '批号'},
                        { field: 'expiration_date', title: '保质期'},
                        { field: 'origin', title: '产地'},
                        { field: 'entry_quantity', title: '入库数量'},
                        { field: 'tax_included_price', title: '含税单价'},
                        { field: 'tax_excluded_price', title: '含税金额'},
                        { field: 'tax_excluded_amount', title: '无税金额'},
                        { field: 'tax_amount', title: '税额'},
                        { field: 'input_tax', title: '进项税'},
                        { field: 'purchase_date', title: '采购日期'},

                    ]],
                    done: function () {
                        var storeButton = $("#store");
                        if (storeButton.length === 0) {
                            console.error('未找到添加按钮元素');
                        } else {
                            storeButton.click(function () {
                                var index = layer.open({
                                    title: '添加耗材',
                                    type: 2,
                                    shade: 0.2,
                                    maxmin: true,
                                    shadeClose: true,
                                    area: ['100%', '100%'],
                                    content: "",
                                });
                                $(window).on("resize", function () {
                                    layer.full(index);
                                });
                            });
                        }
                    }
                });

                upload.render({
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    elem: '#test1',
                    url: "",
                    accept: 'file', // 普通文件
                    data: { "file_id": 2 },
                    done: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.errmsg);
                            window.location.href = '';
                        } else {
                            layer.msg(res.errmsg);
                        }
                    }
                });

                // 监听搜索操作
                $("#Search").click(function () {
                    var customer_name = $("#customer_name").val();
                    var product_code = $("#product_code").val();
                    dataTable.reload({
                        where: { customer_name: customer_name, product_code: product_code,date: $("#date").val() },
                        page: { curr: 1 }
                    });
                });

                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    var data = data.field;
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            searchParams: data
                        }
                    }, 'data');

                    return false;
                });


                // 监听工具条
                table.on('tool(dataTable)', function (obj) {
                    var data = obj.data;
                    var layEvent = obj.event;
                    if (layEvent === 'del') {
                        layer.confirm('确认删除吗？', function (index) {
                            $.post("", { ids: [data.id] }, function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                }
                                layer.close(index);
                                layer.msg(result.msg, { icon: 6 });
                            });
                        });
                    } else if (layEvent === 'edit') {
                        var index = layer.open({
                            title: '编辑',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: "",
                        });
                    } else if (layEvent === 'view') {
                        var index = layer.open({
                            title: '编辑',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: "",
                        });
                    }
                    else if (layEvent === 'submit') {
                        layer.confirm('确认提交审核吗？', function (index) {
                            $.post("", { id: data.id,status:0 }, function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                }
                                layer.close(index);
                                layer.msg(result.msg, { icon: 6 });
                            });
                        });
                    }else if (layEvent === 'permission') {
                        location.href = '/admin/role/' + data.id + '/permission';
                    } else if (layEvent === 'approval') {
                        var index = layer.open({
                            title: '审批',
                            type: 2,
                            shade: 0.2,
                            maxmin: true,
                            shadeClose: true,
                            area: ['50%', '50%'],
                            content: "?id=" + data.id,
                        });
                    } else if (layEvent === 'invoke') {
                        layer.confirm('确认调用数据吗？', function (index) {
                            $.get("", { ids: [data.id] }, function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                }
                                layer.close(index);
                                layer.msg(result.msg, { icon: 6 });
                            });
                        });
                    }
                });

                // 按钮批量删除
                $("#listDelete").click(function () {
                    var ids = [];
                    var hasCheck = table.checkStatus('dataTable');
                    var hasCheckData = hasCheck.data;
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            ids.push(element.id);
                        });
                    }

                    if (ids.length > 0) {
                        layer.confirm('确认选择吗？', function (index) {
                            $.post("", { ids: ids }, function (result) {
                                if (result.code == 0) {
                                    dataTable.reload();
                                }
                                layer.close(index);
                                layer.msg(result.msg, { icon: 6 });
                            });
                        });
                    } else {
                        layer.msg('请选择数据项', { icon: 5 });
                    }
                });
            });
        </script>
    @endcan
@endsection